import java.awt.Image;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;


public class Player {
	
	private int x, dx, y, dy;
	public int nx2;
	private Board b;
	private Image character;
	private ImageIcon iright;
	private ImageIcon ileft;
	private ImageIcon i_still_left, i_still_right;
	
	public Player(Board b) {
		this.b  = b;
		iright = new ImageIcon("Images/character.gif");
		ileft = new ImageIcon("Images/character left.gif");
		i_still_left = new ImageIcon("Images/character still.gif");
		i_still_right = new ImageIcon("Images/character still right.gif");
		character = i_still_right.getImage();
		x = 750; //middle of board
		y = 375; //right position so it looks like he's on the ground
		nx2 = 4096; //background width
	
		
	}
	
	public void move() {
		
		//if moving right
		if (dx != -3) {
			//if player x is on left half
			if (x + dx <= 750) {
				//move player instead of background
				x = x + dx;
			//if player x is on right half
			} else if ( (nx2 > b.getCurrentBackground().getWidth(null) * 1.5) && 
					    ((x + dx) + iright.getIconWidth() < b.getWidth()) ) {
				x = x + dx;
			} else {
				//move background
				if (nx2 < b.getCurrentBackground().getWidth(null) * 1.5) {
					nx2 = nx2 + dx;
				}
			}
		//if moving left
		} else {
			//if player is still on screen
			if (x + dx > 0) {
				//move player instead of background
				x = x + dx;
			}
		}
		
	}
	
	public int getX() {
		return x;
	}
	
	public void setX(int x) {
		this.x = x;
	}
	
	public int getY() {
		return y;
	}
	
	public void setY(int y) {
		this.y = y;
	}
	
	public int getDY() {
		return dy;
	}
	
	
	public Image getImage() {
		return character;
	}
	
	
	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		
		if (key == KeyEvent.VK_LEFT) {
			character = ileft.getImage();
			dx = -3;
			
		}
		
		if (key == KeyEvent.VK_RIGHT) {
			character = iright.getImage();
			dx = 3;
		}
		

		if (key == KeyEvent.VK_UP) {
			character = i_still_right.getImage();
			dy = 1;
		}
		
		
	}
	
	public void keyReleased(KeyEvent e) {
		int key = e.getKeyCode();
		
		if (key == KeyEvent.VK_LEFT) {
			character = i_still_left.getImage();
			dx = 0;
		}
		
		if (key == KeyEvent.VK_RIGHT) {
			character = i_still_right.getImage();
			dx = 0;
		}
		

		if (key == KeyEvent.VK_UP) {
			character = i_still_right.getImage();
			dy = 0;
		}
	}
	

}
